//
//  HMNetworkTool.swift
//  AFN网络封装
//
//  Created by Linkon Y on 16/8/10.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import AFNetworking


//定义一个枚举

enum HMNetworkToolMethod : String {
    //GET请求
    case GET = "GET"
    //POST请求
    case POST = "POST"
}

class HMNetworkTool: AFHTTPSessionManager {
    //全局访问点
    static let sharedTools : HMNetworkTool = {
    
        let tools = HMNetworkTool()
        
//        tools.responseSerializer.acceptableContentTypes?.insert("text/html")
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
    return tools
    
    }()
    
    /**
     网络请求方法
     
     - parameter method:     方法
     - parameter urlStr:     url
     - parameter parameters: 参数
     - parameter success:    成功
     - parameter failure:    失败
     */
    func request(method:HMNetworkToolMethod , urlStr: String , parameters:AnyObject? , success:(response:AnyObject?) -> () , failure:(error:NSError) -> ()){
        //GET请求
        if method == .GET {
            
            GET(urlStr, parameters: parameters, progress: nil, success: { (_, response) -> Void in
                
                success(response: response)
                //7.2与7.3 有区别
                }, failure: { (_, error) in
                    
                    failure(error: error)
            })
            
            
        }else{
            //POSTQ请求
            POST(urlStr, parameters: parameters, progress: nil, success: { (_, response) -> Void in
                
                success(response: response)
                //7.2
                }, failure: { (_, error) in
                    
                    failure(error: error)
                    
            })
            
        }
        
    }
    
}
 // MARK: -  发布页面
extension HMNetworkTool{
    //发布文字微博
    func composeUpdata(status: String , success:(response:AnyObject?) -> () , failure:(error:NSError) -> ()){
        // 请求的url
        let urlString = "https://api.weibo.com/2/statuses/update.json"
        // 请求参数
        let params = [
            "access_token": HMUserAccountViewModel.sharedTools.accessToken!,
            "status": status
        ]
        //发送请求
         request(HMNetworkToolMethod.POST, urlStr: urlString, parameters: params, success: success, failure: failure)
    }
    //发布图片微薄
    func composeUpload(status: String , images:[UIImage], success:(response:AnyObject?) -> () , failure:(error:NSError) -> ()){
        // 请求的urlString
        let urlString = "https://upload.api.weibo.com/2/statuses/upload.json"
        // 请求参数
        let params = [
            "access_token": HMUserAccountViewModel.sharedTools.accessToken!,
            "status": status
            ]
        //发送请求
        POST(urlString, parameters: params, constructingBodyWithBlock: { (formdata) in
            //二进制
            let data = UIImagePNGRepresentation(images.first!)!
            formdata.appendPartWithFileData(data, name: "pic", fileName: "asd", mimeType: "application/octet-stream")
            }, progress: nil, success: { (_, response) in
                success(response:response)
            }) { (_, error) in
                failure(error: error)
        }
    }
}
 // MARK: - 首页界面
extension HMNetworkTool{
    //
    func homeLoadData(sinceId:Int64, maxId:Int64, success:(response:AnyObject?) -> () , failure:(error:NSError) -> ()){
        //请求Url
    let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
        //请求参数
    let params = [
        "access_token": HMUserAccountViewModel.sharedTools.userAccount!.access_token!,
        "since_id":"\(sinceId)",
        "max_id":"\(maxId)"
        ]
        request(HMNetworkToolMethod.GET, urlStr: urlString, parameters: params, success: success, failure: failure)
    }
}


 // MARK: - 授权界面
extension HMNetworkTool{
    //请求授权
    func oauthLoadUserAccount(code:String ,success:(response:AnyObject?) -> () , failure:(error:NSError) -> ()){
        //请求url
        let urlString = "https://api.weibo.com/oauth2/access_token"
        //请求参数
        let params = [
            "client_id":WBAppKey,
            "client_secret": WBAppSecret,
            "grant_type":"authorization_code",
            "code":code,
            "redirect_uri": WBRedirect_uri
        ]
        request(HMNetworkToolMethod.POST, urlStr: urlString, parameters: params, success: success, failure: failure)
    }
    
    //请求个人信息呢
    func oauthLoadUserInfo(userAccount:HMUserAccount_Model,success:(response:AnyObject?) -> () , failure:(error:NSError) -> ()){
        //请求URL
        let urlString = "https://api.weibo.com/2/users/show.json"
        //参数
        let params = [
            "access_token": userAccount.access_token!,
            "uid":userAccount.uid!
        ]
       request(HMNetworkToolMethod.GET, urlStr: urlString, parameters: params, success: success, failure: failure)
    }
}



 // MARK: - url接口
extension HMNetworkTool{
    
    func gerUrlData(success:(response:AnyObject?) -> () , failure:(error:NSError) -> ()){
        
        let url = "http://www.weather.com.cn/data/sk/101010100.html"
        
        request(HMNetworkToolMethod.GET, urlStr: url, parameters: nil, success: success, failure: failure)
    
    }
    
}
